package com.aion.jirc.gui;

import javax.swing.JEditorPane;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import java.awt.BorderLayout;
import java.awt.event.KeyListener;

/**
 * Created by IntelliJ IDEA.
 * Date: 11-01-29
 *
 * @author Aion
 * @author Zippy
 */
public abstract class Window extends JInternalFrame {

    private String windowName;
    private boolean isChannel;
    private boolean hasOutputEnabled;

    private JEditorPane inputArea;
    private JList usersList;
    private JTextField outputArea;

    private KeyListener kl;

    protected Window(KeyListener kl, String windowName, boolean hasInputEnabled, boolean isChannel) {
        super("", true, true, true, true);
        this.windowName = windowName;
        this.hasOutputEnabled = hasInputEnabled;
        this.kl = kl;
        this.isChannel = isChannel;
        initComponents();
    }

    /**
     * Adds a line to the input area.
     * @param line the line to add
     */
    public synchronized void addLine(String line) {
        JEditorPane inputArea = getInputArea();
        if (inputArea != null) {
            String[] text = inputArea.getText().split("</body>");
            StringBuffer input = new StringBuffer();
            input.append(text[0]);
            if (!text[0].split("<body>")[1].isEmpty()) {
                input.append("<br />");
            }
            input.append(line);
            input.append("</body></html>");
            inputArea.setText(input.toString());
        }
    }

    /**
     * Gets the input area.
     * @return the input area which is a <code>JEditorPane</code>
     */
    public final JEditorPane getInputArea() {
        return inputArea;
    }

    /**
     * Gets the output area.
     * @return the output area which is a <code>JTextField</code> or null if this window is not a channel
     */
    public final JTextField getOutputArea() {
        return outputArea;
    }

    /**
     * Gets the users list.
     * @return the users' list which is a <code>JList</code> or null if this window is not a channel
     */
    public final JList getUsersList() {
        return usersList;
    }

    /**
     * Gets the window's name
     * @return the window's name
     */
    public final String getWindowName() {
        return windowName;
    }

    /**
     * Returns whether or not this window has output enabled.
     * @return <tt>true</tt> if output is enabled; otherwise <tt>false</tt>
     */
    public final boolean hasOutputEnabled() {
        return hasOutputEnabled;
    }

    /**
     * Returns whether or not this window is a channel.
     * @return <tt>true</tt> if it is a channel; otherwise <tt>false</tt>
     */
    public final boolean isChannel() {
        return isChannel;
    }

    /**
     * Processes the line before adding it to the input area.
     * @param line
     *          the line to process
     */
    public abstract void processLine(String line);

    /**
     * Initializes the components
     */
    private void initComponents() {
        setTitle(getWindowName());
        inputArea = new JEditorPane("text/html", "");
        inputArea.setEditable(false);
        JScrollPane inputScrollPane = new JScrollPane(inputArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        inputScrollPane.setBorder(null);
        inputScrollPane.setAutoscrolls(true);
        if (isChannel()) {
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setResizeWeight(1);
            splitPane.add(inputScrollPane);
            usersList = new JList();
            JScrollPane listScrollPane = new JScrollPane(usersList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            listScrollPane.setBorder(null);
            splitPane.add(listScrollPane);
            add(splitPane,  BorderLayout.CENTER);
        } else {
            add(inputScrollPane);
        }
        if (hasOutputEnabled()) {
            outputArea = new JTextField();
            outputArea.addKeyListener(kl);
            add(outputArea, BorderLayout.PAGE_END);
        }
        setVisible(true);
        setResizable(true);
    }
}
